Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern int SockId;
- SqSocket *FirstSock = NULL;
- void RunSock() {
- for(SqSocket *Sock = FirstSock; Sock;) {
- int RemoveSocket = 0;
- SqSocket *Next = Sock->Next;
- if(Sock->Flags & SQSOCK_CONNECTED) {
- char WriteHere[0x10000];
- *WriteHere = 0;
- int Written = BIO_read(Sock->Bio, WriteHere, sizeof(WriteHere));
- WriteHere[Written]=0;
- if(Written > 0) {
- RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_NEW_DATA, WriteHere);
- }
- } else {
- int TryConnect = BIO_do_connect(Sock->Bio);
- if(TryConnect == 1) {
- RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_CONNECTED, "");
- Sock->Flags |= SQSOCK_CONNECTED;
- } else if(!TryConnect) {
- RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_CANT_CONNECT, "Can't connect");
- RemoveSocket = 1;
- } else {
- Sock->Retries++;
- if(Sock->Retries > 20) {
- RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_CANT_CONNECT, "Connection timed out");
- RemoveSocket = 1;
- }
- }
- }
- if(RemoveSocket)
- DeleteSocketById(Sock->Id);
- Sock = Next;
- }
- }
- // http://wiki.openssl.org/index.php/BIO
- void InitSock() {
- SSL_library_init();
- SSL_load_error_strings();
- ERR_load_BIO_strings();
- ERR_load_crypto_strings();
- OpenSSL_add_all_algorithms();
- SSLContext = SSL_CTX_new(SSLv23_client_method());
- SSL_CTX_set_verify(SSLContext, SSL_VERIFY_NONE, NULL);
- }
- void EndSock() {
- EVP_cleanup();
- CRYPTO_cleanup_all_ex_data();
- ERR_free_strings();
- SSL_CTX_free(SSLContext);
- }
- void RunSqSockHandle(HSQUIRRELVM v, HSQOBJECT Handler, int Id, int Event, const char *Text) {
- SQInteger top = sq_gettop(v);
- sq_pushobject(v, Handler);
- sq_pushroottable(v);
- sq_pushinteger(v,Id);
- sq_pushinteger(v,Event);
- sq_pushstring(v, Text, -1);
- sq_call(v, 4, SQFalse,SQTrue);
- sq_settop(v, top);
- }
- int CreateSqSock(HSQUIRRELVM v, HSQOBJECT Handler, const char *Host, int Flags) {
- char Host2[strlen(Host)+1];
- strcpy(Host2, Host);
- BIO *Bio = BIO_new_connect(Host2);
- if(!Bio) {
- RunSqSockHandle(v, Handler, SockId, SQS_CANT_CONNECT, "Can't create bio");
- return 0;
- }
- BIO_set_nbio(Bio, 1);
- struct SqSocket *Socket = (struct SqSocket*)calloc(1,sizeof(struct SqSocket));
- if(!Socket) return 0;
- Socket->Flags = Flags;
- Socket->Bio = Bio;
- Socket->Script = v;
- Socket->Function = Handler;
- Socket->Id = SockId;
- if(FirstSock)
- FirstSock->Prev = Socket;
- sq_addref(v, &Handler);
- FirstSock = Socket;
- return 1;
- }
- void DeleteSocketById(int Id) {
- for(SqSocket *Sock = FirstSock; Sock; Sock=Sock->Next)
- if(Sock->Id == Id) {
- if(FirstSock == Sock)
- FirstSock = Sock->Next;
- sq_release(Sock->Script, &Sock->Function);
- BIO_free(Sock->Bio);
- free(Sock);
- return;
- }
- }
- SQInteger Sq_NetSend(HSQUIRRELVM v) {
- SQInteger Id; const SQChar *Text;
- sq_getinteger(v, 2, &Id);
- sq_getstring(v, 3, &Text);
- for(SqSocket *Sock = FirstSock; Sock; Sock=Sock->Next)
- if(Sock->Id == Id) {
- BIO_puts(Sock->Bio, Text);
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement