예제 #1
0
파일: Socket.cs 프로젝트: liulilittle/nsjs
 private static void ReceiveAsync(IntPtr info)
 {
     InternalReceiveAsync(info, false, (socket, socketobject, data, buffer, ofs, count, flags, remoteep, callback) =>
     {
         bool success = false;
         if ((success = SocketExtension.BeginReceive(socket, buffer, ofs, count, flags, (result) =>
         {
             int len = SocketExtension.EndReceive(socket, result, out SocketError error);
             NSJSVirtualMachine machine = socketobject.VirtualMachine;
             machine.Join((sender, state) =>
             {
                 if (len > 0)
                 {
                     for (int i = ofs; i < len; i++)
                     {
                         data[i] = buffer[i];
                     }
                 }
                 if (callback != null)
                 {
                     callback.Call(socketobject, NSJSInt32.New(machine, unchecked ((int)error)), NSJSInt32.New(machine, len));
                 }
             });
         })))
         {
             if (callback != null)
             {
                 callback.CrossThreading = true;
             }
             data.CrossThreading = true;
         }
         return(success);
     });
예제 #2
0
 private unsafe void ProcessReceive(IAsyncResult result)
 {
     if (_buffer == null)
     {
         _buffer = new byte[SocketExtension.MSS];
     }
     if (result == null)
     {
         int len = SocketExtension.MSS;
         if (frame != null)
         {
             long surplus = frame.payload_surplus;
             if (surplus < len)
             {
                 len = (int)surplus;
             }
         }
         if (!SocketExtension.BeginReceive(socket, _buffer, 0, len,
                                           ProcessReceive))
         {
             CloseOrError(true);
         }
     }
     else
     {
         int len = SocketExtension.EndReceive(socket, result);
         if (len <= 0)
         {
             CloseOrError(false);
         }
         else
         {
             fixed(byte *pinned = _buffer)
             {
                 ProcessReceive(pinned, len);
             }
         }
     }
 }