Пример #1
0
        private void ConnectCallback(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;

            try
            {
                client.EndConnect(ar);
            }
            catch (SocketException x)
            {
                if (connection != null)
                {
                    connection.Disconnect();
                }
                if (NetConfig.logFilter >= JHSLogFilter.Developer)
                {
                    JHSDebug.Log("JHSNetworkManager :: Excepiton:" + x.GetErrorCode());
                }
                Connecting = false;
                return;
            }

            catch (Exception e)
            {
                if (connection != null)
                {
                    connection.Disconnect();
                }
                if (NetConfig.logFilter >= JHSLogFilter.Developer)
                {
                    JHSDebug.Log("JHSNetworkManager :: Excepiton:" + e.ToString());
                }
                Connecting = false;
                return;
            }
            try
            {
                connection.SetHandlers(m_MessageHandlers);
                connection.StartReceiving(client);
                HandShakeMsg packet = new HandShakeMsg
                {
                    Version = NetConfig.Version,
                    OP      = 0,
                };
                connection.Send(InternalMessages.HeandShake_Server, packet);
                Connecting     = false;
                Connected      = true;
                m_RecconectTry = 0;
            }
            catch (Exception e)
            {
                if (NetConfig.logFilter >= JHSLogFilter.Developer)
                {
                    JHSDebug.Log("JHSNetworkManager :: Excepiton:" + e.ToString());
                }
                Connecting = false;
            }
        }
        internal void HandleClientHandShake(JHSNetworkMessage netMsg)
        {
            HandShakeMsg packet = netMsg.ReadMessage <HandShakeMsg>();

            if (packet != null)
            {
                if (packet.OP == 0) //VER VERSION
                {
                    netMsg.conn.connectionId = packet.Version;
                    netMsg.conn.stage        = PerStage.Connecting;
                    HandShakeMsg p = new HandShakeMsg
                    {
                        Version = (uint)NetConfig.Key,
                        OP      = 1
                    };
                    netMsg.conn.Send(InternalMessages.HeandShake_Server, p);
                }
                else if (packet.OP == 1)
                {
                    if (packet.Version == NetConfig.Key)
                    {
                        netMsg.conn.stage = PerStage.Connected;
                        HandShakeMsg p = new HandShakeMsg
                        {
                            Version = (uint)NetConfig.Key,
                            OP      = 2
                        };
                        netMsg.conn.Send(InternalMessages.HeandShake_Server, p);
                        ClientConnected = true;
                        connection      = netMsg.conn;
                        PushMessage(new JHSNetworkMessage
                        {
                            msgType = InternalMessages.CONNECTED,
                            conn    = netMsg.conn,
                            reader  = new JHSNetworkReader()
                        });
                    }
                }
            }
        }
Пример #3
0
        internal void HandleServerHandShake(JHSNetworkMessage netMsg)
        {
            HandShakeMsg packet = netMsg.ReadMessage <HandShakeMsg>();

            if (packet != null)
            {
                if (packet.OP == 0) //VER VERSION
                {
                    if (packet.Version == NetConfig.Version)
                    {
                        netMsg.conn.stage = PerStage.Verifying;
                        HandShakeMsg p = new HandShakeMsg
                        {
                            Version = netMsg.conn.connectionId,
                            OP      = 0
                        };
                        netMsg.conn.Send(InternalMessages.HeandShake_Client, p);
                    }
                    else
                    {
                        netMsg.conn.Disconnect();
                    }
                }
                else if (packet.OP == 1)
                {
                    if (packet.Version == NetConfig.Key)
                    {
                        netMsg.conn.stage = PerStage.Connecting;
                        HandShakeMsg p = new HandShakeMsg
                        {
                            Version = (uint)NetConfig.Key,
                            OP      = 1
                        };
                        netMsg.conn.Send(InternalMessages.HeandShake_Client, p);
                    }
                    else
                    {
                        netMsg.conn.Disconnect();
                    }
                }
                else if (packet.OP == 2)
                {
                    if (packet.Version == NetConfig.Key)
                    {
                        netMsg.conn.stage = PerStage.Connected;
                        if (NetConfig.logFilter >= JHSLogFilter.Log)
                        {
                            JHSDebug.Log("JHSNetworkManager :: Connected:" + netMsg.conn.connectionId);
                        }
                        AddConnection(netMsg.conn);
                    }
                    else
                    {
                        netMsg.conn.Disconnect();
                    }
                }
                else
                {
                    netMsg.conn.Disconnect();
                }
            }
        }