sock() public method

public sock ( ) : Socket
return Socket
        private void _asyncReceive()
        {
            if (_networkInterface == null || !_networkInterface.valid())
            {
                Dbg.WARNING_MSG("PacketReceiver::_asyncReceive(): network interface invalid!");
                return;
            }

            var socket = _networkInterface.sock();

            while (true)
            {
                // 必须有空间可写,否则我们阻塞在线程中直到有空间为止
                int first = 0;
                int space = _free();

                while (space == 0)
                {
                    if (first > 0)
                    {
                        if (first > 1000)
                        {
                            Dbg.ERROR_MSG("PacketReceiver::_asyncReceive(): no space!");
                            Event.fireIn("_closeNetwork", new object[] { _networkInterface });
                            return;
                        }

                        Dbg.WARNING_MSG("PacketReceiver::_asyncReceive(): waiting for space, Please adjust 'RECV_BUFFER_MAX'! retries=" + first);
                        System.Threading.Thread.Sleep(5);
                    }

                    first += 1;
                    space  = _free();
                }

                int bytesRead = 0;
                try
                {
                    bytesRead = socket.Receive(_buffer, _wpos, space, 0);
                }
                catch (SocketException se)
                {
                    Dbg.ERROR_MSG(string.Format("PacketReceiver::_asyncReceive(): receive error, disconnect from '{0}'! error = '{1}'", socket.RemoteEndPoint, se));
                    Event.fireIn("_closeNetwork", new object[] { _networkInterface });
                    return;
                }

                if (bytesRead > 0)
                {
                    // 更新写位置
                    Interlocked.Add(ref _wpos, bytesRead);
                }
                else
                {
                    Dbg.WARNING_MSG(string.Format("PacketReceiver::_asyncReceive(): receive 0 bytes, disconnect from '{0}'!", socket.RemoteEndPoint));
                    Event.fireIn("_closeNetwork", new object[] { _networkInterface });
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        public void startRecv()
        {
            // 必须有空间可写,否则我们阻塞在线程中直到有空间为止
            int first = 0;
            int space = _free();

            while (space == 0)
            {
                if (first > 0)
                {
                    if (first > 1000)
                    {
                        throw new Exception("PacketReceiver::startRecv(): no space!");
                    }

                    Dbg.WARNING_MSG("PacketReceiver::startRecv(): waiting for space, Please adjust 'RECV_BUFFER_MAX'! retries=" + first);
                    System.Threading.Thread.Sleep(5);
                }

                first += 1;
                space  = _free();
            }

            try
            {
                _networkInterface.sock().BeginReceive(_buffer, _wpos, space, 0,
                                                      _asyncCallback, this);
            }
            catch (Exception e)
            {
                Dbg.ERROR_MSG("PacketReceiver::startRecv(): call ReceiveAsync() is err: " + e.ToString());
                Event.fireIn("_closeNetwork", new object[] { _networkInterface });
            }
        }
Ejemplo n.º 3
0
        void _startSend()
        {
            int sendSize = Interlocked.Add(ref _wpos, 0) - _spos;
            int t_spos   = _spos % _buffer.Length;

            if (t_spos == 0)
            {
                t_spos = sendSize;
            }

            if (sendSize > _buffer.Length - t_spos)
            {
                sendSize = _buffer.Length - t_spos;
            }

            try
            {
                _networkInterface.sock().BeginSend(_buffer, _spos % _buffer.Length, sendSize, 0,
                                                   new AsyncCallback(_onSent), this);
            }
            catch (Exception e)
            {
                Dbg.ERROR_MSG("PacketSender::startSend(): is err: " + e.ToString());
                Event.fireIn("_closeNetwork", new object[] { _networkInterface });
            }
        }
        public void startRecv()
        {
            // 必须有空间可写,否则我们阻塞在线程中直到有空间为止
            int first = 0;
            int space = _free();

            while (space == 0)
            {
                if (first > 0)
                {
                    Dbg.WARNING_MSG("PacketReceiver::startRecv(): waiting for space! retries=" + first);
                    System.Threading.Thread.Sleep(5);
                }

                first += 1;
                space  = _free();
            }

            try
            {
                _networkInterface.sock().BeginReceive(_buffer, _wpos, space, 0,
                                                      new AsyncCallback(_onRecv), this);
            }
            catch (Exception e)
            {
                Dbg.ERROR_MSG("PacketReceiver::startRecv(): call ReceiveAsync() is err: " + e.ToString());
                _networkInterface.close();
            }
        }
Ejemplo n.º 5
0
        void _asyncSend()
        {
            if (_networkInterface == null || !_networkInterface.valid())
            {
                Dbg.WARNING_MSG("PacketSender::_asyncSend(): network interface invalid!");
                return;
            }

            var socket = _networkInterface.sock();

            while (true)
            {
                Monitor.Enter(_sendingObj);

                int sendSize = _wpos - _spos;
                int t_spos   = _spos % _buffer.Length;
                if (t_spos == 0)
                {
                    t_spos = sendSize;
                }

                if (sendSize > _buffer.Length - t_spos)
                {
                    sendSize = _buffer.Length - t_spos;
                }

                int bytesSent = 0;
                try
                {
                    bytesSent = socket.Send(_buffer, _spos % _buffer.Length, sendSize, 0);
                }
                catch (SocketException se)
                {
                    Dbg.ERROR_MSG(string.Format("PacketSender::_asyncSend(): send data error, disconnect from '{0}'! error = '{1}'", socket.RemoteEndPoint, se));
                    Event.fireIn("_closeNetwork", new object[] { _networkInterface });

                    Monitor.Exit(_sendingObj);
                    return;
                }

                _spos += bytesSent;

                // 所有数据发送完毕了
                if (_spos == _wpos)
                {
                    _sending = false;
                    Monitor.Exit(_sendingObj);
                    return;
                }

                Monitor.Exit(_sendingObj);
            }
        }
Ejemplo n.º 6
0
 static int sock(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         KBEngine.NetworkInterface obj = (KBEngine.NetworkInterface)ToLua.CheckObject <KBEngine.NetworkInterface>(L, 1);
         System.Net.Sockets.Socket o   = obj.sock();
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
        private static void connectCB(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                NetworkInterface networkInterface = (NetworkInterface)ar.AsyncState;

                // Complete the connection.
                networkInterface.sock().EndConnect(ar);

                Event.fireIn("_onConnectStatus", new object[] { "" });
            }
            catch (Exception e)
            {
                Event.fireIn("_onConnectStatus", new object[] { e.ToString() });
            }
        }