示例#1
0
        static void ConnectCallback(IAsyncResult ar)
        {
            var s = (ConnStateObject)ar.AsyncState;

            try
            {
                s.workSocket.EndConnect(ar);
                if (s.workSocket.Connected)
                {
                    var chan = new Chan(
                        (data) => {
                        SocketSendingAndReceiving.Send(s.workSocket, data);
                    });
                    s.onConnected(chan);
                    SocketSendingAndReceiving.BeginReceive(
                        s.workSocket,
                        (data, bytesReceived) =>
                    {
                        if (bytesReceived > 0)
                        {
                            chan.HandleRecv(data, bytesReceived);
                        }
                        else
                        {
                            chan.Break(Chan.BreakReason.ReceiveZero);
                        }
                    });
                }
                else
                {
                    Debug.Log("Client not connected!");
                    s.onConnected(null);
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Exception on connecting callback " + e.Message);
                s.onConnected(null);
            }
        }
示例#2
0
        static void AcceptCallback(IAsyncResult ar)
        {
            allDone.Set();
            var    s       = (AcceptStateObject)ar.AsyncState;
            Socket handler = null;

            lock (socketLock)
            {
                if (socket != null)
                {
                    handler = socket.EndAccept(ar);
                }
            }
            if (handler == null)
            {
                s.onConnected(null);
                return;
            }
            Debug.Log("Client connectioned");
            var chan = new Chan(
                (data) => {
                SocketSendingAndReceiving.Send(handler, data);
            });

            s.onConnected(chan);
            SocketSendingAndReceiving.BeginReceive(
                handler,
                (data, bytesReceived) =>
            {
                if (bytesReceived > 0)
                {
                    chan.HandleRecv(data, bytesReceived);
                }
                else
                {
                    chan.Break(Chan.BreakReason.ReceiveZero);
                }
            });
        }