Advertisement
NovaYoshi

socket stuff

Oct 27th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 KB | None | 0 0
  1. extern int SockId;
  2. SqSocket *FirstSock = NULL;
  3.  
  4. void RunSock() {
  5.   for(SqSocket *Sock = FirstSock; Sock;) {
  6.     int RemoveSocket = 0;
  7.     SqSocket *Next = Sock->Next;
  8.     if(Sock->Flags & SQSOCK_CONNECTED) {
  9.       char WriteHere[0x10000];
  10.       *WriteHere = 0;
  11.       int Written = BIO_read(Sock->Bio, WriteHere, sizeof(WriteHere));
  12.       WriteHere[Written]=0;
  13.       if(Written > 0) {
  14.         RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_NEW_DATA, WriteHere);
  15.       }
  16.     } else {
  17.       int TryConnect = BIO_do_connect(Sock->Bio);
  18.       if(TryConnect == 1) {
  19.         RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_CONNECTED, "");
  20.         Sock->Flags |= SQSOCK_CONNECTED;
  21.       } else if(!TryConnect) {
  22.         RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_CANT_CONNECT, "Can't connect");
  23.         RemoveSocket = 1;
  24.       } else {
  25.         Sock->Retries++;
  26.         if(Sock->Retries > 20) {
  27.           RunSqSockHandle(Sock->Script, Sock->Function, Sock->Id, SQS_CANT_CONNECT, "Connection timed out");
  28.           RemoveSocket = 1;
  29.         }
  30.       }
  31.     }
  32.     if(RemoveSocket)
  33.       DeleteSocketById(Sock->Id);
  34.     Sock = Next;
  35.   }
  36. }
  37.  
  38. // http://wiki.openssl.org/index.php/BIO
  39. void InitSock() {
  40.   SSL_library_init();
  41.   SSL_load_error_strings();
  42.   ERR_load_BIO_strings();
  43.   ERR_load_crypto_strings();
  44.   OpenSSL_add_all_algorithms();
  45.  
  46.   SSLContext = SSL_CTX_new(SSLv23_client_method());
  47.   SSL_CTX_set_verify(SSLContext, SSL_VERIFY_NONE, NULL);
  48. }
  49.  
  50. void EndSock() {
  51.   EVP_cleanup();
  52.   CRYPTO_cleanup_all_ex_data();
  53.   ERR_free_strings();
  54.   SSL_CTX_free(SSLContext);
  55. }
  56.  
  57. void RunSqSockHandle(HSQUIRRELVM v, HSQOBJECT Handler, int Id, int Event, const char *Text) {
  58.   SQInteger top = sq_gettop(v);
  59.   sq_pushobject(v, Handler);
  60.   sq_pushroottable(v);
  61.   sq_pushinteger(v,Id);
  62.   sq_pushinteger(v,Event);
  63.   sq_pushstring(v, Text, -1);
  64.   sq_call(v, 4, SQFalse,SQTrue);
  65.   sq_settop(v, top);
  66. }
  67.  
  68. int CreateSqSock(HSQUIRRELVM v, HSQOBJECT Handler, const char *Host, int Flags) {
  69.   char Host2[strlen(Host)+1];
  70.   strcpy(Host2, Host);
  71.   BIO *Bio = BIO_new_connect(Host2);
  72.   if(!Bio) {
  73.     RunSqSockHandle(v, Handler, SockId, SQS_CANT_CONNECT, "Can't create bio");
  74.     return 0;
  75.   }
  76.   BIO_set_nbio(Bio, 1);
  77.  
  78.   struct SqSocket *Socket = (struct SqSocket*)calloc(1,sizeof(struct SqSocket));
  79.   if(!Socket) return 0;
  80.   Socket->Flags = Flags;
  81.   Socket->Bio = Bio;
  82.   Socket->Script = v;
  83.   Socket->Function = Handler;
  84.   Socket->Id = SockId;
  85.   if(FirstSock)
  86.     FirstSock->Prev = Socket;
  87.   sq_addref(v, &Handler);
  88.   FirstSock = Socket;
  89.   return 1;
  90. }
  91.  
  92. void DeleteSocketById(int Id) {
  93.   for(SqSocket *Sock = FirstSock; Sock; Sock=Sock->Next)
  94.     if(Sock->Id == Id) {
  95.       if(FirstSock == Sock)
  96.         FirstSock = Sock->Next;
  97.       sq_release(Sock->Script, &Sock->Function);
  98.       BIO_free(Sock->Bio);
  99.       free(Sock);
  100.       return;
  101.     }
  102. }
  103.  
  104. SQInteger Sq_NetSend(HSQUIRRELVM v) {
  105.   SQInteger Id; const SQChar *Text;
  106.   sq_getinteger(v, 2, &Id);
  107.   sq_getstring(v, 3, &Text);
  108.  
  109.   for(SqSocket *Sock = FirstSock; Sock; Sock=Sock->Next)
  110.     if(Sock->Id == Id) {
  111.       BIO_puts(Sock->Bio, Text);
  112.       return 0;
  113.     }
  114.   return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement