Exemplo n.º 1
0
    public void Connect()
    {
        socket = new WebSocket($"wss://{server}/socket.io/?EIO=3&transport=websocket")
        {
            Log = { Level = LogLevel.Trace, File = @"C:\Users\solca\RiderProjects\prco204-supine\wslog.txt" }
        };


        socket.OnClose += (sender, args) => {
            OnSocketDisconnected?.Invoke(String.Format("[{0}] {1}", args.Code, args.Reason));
            StopNetworkTick();
        };

        socket.OnMessage += (sender, e) => DecodeMessage(e.Data);

        socket.OnOpen += (sender, e) => {
            Debug.Log("Server connection established");
            InvokeRepeating("SendHeartbeat", 25, 25); // Heartbeat every 25s after 25s
            OnSocketConnected?.Invoke();
            //StartNetworkTick();
        };

        socket.OnError += (sender, e) => {
            OnSocketError?.Invoke(e.Message + " - " + e.Exception.StackTrace);
            StopNetworkTick();
            throw e.Exception;
        };

        socket.Connect();
    }
Exemplo n.º 2
0
        internal byte[] ReceiveData()
        {
            try
            {
                //Orignally I thought about clearing the buffer here, but I changed
                //my mind since you should never get more bytes in the returned
                //buffer than what you receive.

                SessionSocket.ReceiveTimeout = MillisecondTimeout;
                int bytesRecvd = SessionSocket.Receive(_receiveBuffer);


                byte[] retVal = new byte[bytesRecvd];
                Buffer.BlockCopy(_receiveBuffer, 0, retVal, 0, bytesRecvd);

                return(retVal);
            }
            catch (SocketException se)
            {
                //The connection has timed out probably
                OnSocketError?.Invoke(se);
            }

            return(null);
        }
Exemplo n.º 3
0
    //-------------------------------------------------------------------------
    void _onSocketError(object rec, SocketErrorEventArgs args)
    {
        //this.tcpSocket = null;

        if (OnSocketError != null)
        {
            OnSocketError.Invoke(this, args);
        }
    }
Exemplo n.º 4
0
        public override void SocketOnError(object sender, Exception ex)
        {
            base.SocketOnError(sender, ex);

            if (this.m_StreamStarted && !this.m_CleanUpDone)
            {
                this.CleanupSession();
            }

            OnSocketError?.Invoke(this, ex);
        }
Exemplo n.º 5
0
        internal int SendData(byte[] data)
        {
            if (!Connected)
            {
                return(-1);
            }

            try
            {
                return(SessionSocket.Send(data));
            }
            catch (SocketException se)
            {
                OnSocketError?.Invoke(se);
            }

            return(-1);
        }
Exemplo n.º 6
0
 private void Receive()
 {
     _socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref ep, recv = (ar) =>
     {
         StateObject so = (StateObject)ar.AsyncState;
         try
         {
             int bytes   = _socket.EndReceiveFrom(ar, ref ep);
             byte[] data = new byte[bytes];
             Buffer.BlockCopy(so.buffer, 0, data, 0, bytes);
             _socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref ep, recv, so);
             PacketReceive((IPEndPoint)ep, data, bytes);
         }
         catch (Exception ex)
         {
             OnSocketError?.Invoke((IPEndPoint)ep, ex);
         }
     }, state);
 }
Exemplo n.º 7
0
 public void SendBytes(IPEndPoint endPoint, byte[] data)
 {
     PacketSending(endPoint, data, data.Length);
     try
     {
         _socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, endPoint, (ar) =>
         {
             try
             {
                 StateObject so = (StateObject)ar.AsyncState;
                 int bytes      = _socket.EndSend(ar);
             }
             catch (Exception ex)
             {
                 OnSocketError?.Invoke(endPoint, ex);
             }
         }, state);
     }
     catch (Exception ex)
     {
         OnSocketError?.Invoke(endPoint, ex);
     }
 }
Exemplo n.º 8
0
 private void _ws_OnError(object sender, WebSocketSharp.ErrorEventArgs e)
 {
     OnSocketError?.Invoke(this, new SocketErrorException(e.Exception, e.Message));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Event handler for socket error
 /// </summary>
 private void Transport_OnSocketError(string mess)
 {
     OnSocketError?.Invoke(mess);
 }
Exemplo n.º 10
0
        /// <summary>
        /// Event handler for socket error
        /// </summary>
        private void Socket_OnError(object sender, ErrorEventArgs e)
        {
            logger.Trace("{0}", JsonConvert.SerializeObject(e));

            OnSocketError?.Invoke(e.Message);
        }
Exemplo n.º 11
0
 public override void SocketError(IPEndPoint ep, Exception ex)
 {
     base.SocketError(ep, ex);
     OnSocketError?.Invoke(ep, ex);
 }
Exemplo n.º 12
0
        private void _OnCompleted(object sender, SocketAsyncEventArgs args)
        {
            //try
            {
                switch (args.LastOperation)
                {
                case SocketAsyncOperation.Accept:    //accept a connect
                {
                    if (args.SocketError != SocketError.Success)
                    {
                        //直接复用
                        args.AcceptSocket = null;
                        listenSocket.AcceptAsync(args);
                    }
                    if (args.SocketError == SocketError.Success)
                    {
                        var info = new Link(args.AcceptSocket, null);
                        Connects[info.Handle] = info;

                        //直接复用
                        args.AcceptSocket = null;
                        listenSocket.AcceptAsync(args);
                        if (args.SocketError == SocketError.Success)
                        {
                            OnSocketAccept?.Invoke(info.Handle);
                            SetRecivce(info);
                        }
                    }
                }
                    return;

                case SocketAsyncOperation.Connect:    //connect succ
                {
                    if (args.SocketError != SocketError.Success)
                    {
                        OnSocketError?.Invoke(args.UserToken as Socket);
                        args.Dispose();
                    }
                    else
                    {
                        var info = new Link(args.ConnectSocket, args);
                        Connects[info.Handle] = info;

                        OnSocketLinked?.Invoke(info.Handle);

                        SetRecivce(info);
                    }
                    //connect 的这个args不能复用
                }
                    return;

                case SocketAsyncOperation.Send:
                {
                    var hash = (args.UserToken as Link).Handle;
                    if (args.SocketError != SocketError.Success)
                    {
                        ReuseSocketAsyncEventArgs(args);
                        //断链,复用这个接受参数
                        CloseConnect(hash);
                    }
                    else
                    {
                        //发送成功,也复用这个发送参数
                        ReuseSocketAsyncEventArgs(args);
                        //断链,复用这个接受参数
                        OnSocketSend?.Invoke(hash);
                    }
                }
                    return;

                case SocketAsyncOperation.Receive:
                {
                    var hash = (args.UserToken as Link).Handle;
                    if (args.BytesTransferred == 0 || args.SocketError != SocketError.Success)
                    {
                        ReuseRecvSocketAsyncEventArgs(args);        //断链,复用这个接受参数
                        CloseConnect(hash);
                        return;
                    }
                    else
                    {
                        byte[] recv = new byte[args.BytesTransferred];
                        Buffer.BlockCopy(args.Buffer, args.Offset, recv, 0, args.BytesTransferred);
                        (args.UserToken as Link).Socket.ReceiveAsync(args);        //直接复用
                        OnSocketRecv?.Invoke(hash, recv);
                    }
                }
                    return;

                default:
                {
                    break;
                }
                }
            }
        }