public void SendToServer(Packet rawPacket) { if (serverConnection == null) { return; } var filteredPacket = clientPacketHandler.FilterOutput(rawPacket); if (filteredPacket.HasValue) { lock (serverStreamLock) { using (var memoryStream = new MemoryStream(1024)) { serverConnection.Send(filteredPacket.Value, memoryStream); var buffer = memoryStream.GetBuffer(); #if DUMP_RAW Console.Info("proxy -> server"); Console.Info(buffer.Take((int)memoryStream.Length).Select(x => x.ToString("X2")).Aggregate((l, r) => l + " " + r)); #endif ServerStream.Write(buffer, 0, (int)memoryStream.Length); } } } }
public async Task Listen_ServerRespondsWithCommands_ReturnsCommands() { // arrange var response = "" + "TS3\r\n" + "Welcome to the TeamSpeak 3 ServerQuery interface.\r\n" + "channelcreate channel_name=test channel_order=1 channel_needed_talk_power=200 channel_flag_permanent=0\r\n" + "cid=21\r\n" + "error id=0 msg=ok\r\n"; var server = new ServerStream { Response = response }; // act var client = new Client((_, __) => Task.FromResult <Stream>(server)); await client.Connect(); var listener = client.Listen(); var commands = await ReceiveExact(listener, 3); client.Disconnect(); // assert Assert.Equal("channelcreate", commands[0].Name); Assert.Equal("21", commands[1].GetParameter("cid")); Assert.Equal("error", commands[2].Name); Assert.Equal("ok", commands[2].GetParameter("msg")); }
public bool SendDataToServer(DataPacket dpDataToSend) { bool IsSent = false; try { string data = string.Empty; data = JsonConvert.SerializeObject(dpDataToSend) + "|"; data = JsonConvert.SerializeObject(dpDataToSend); byte[] ByteData = Encoding.ASCII.GetBytes(data + "|"); if (Client.Connected && ServerStream.CanWrite) { ServerStream.Write(ByteData, 0, ByteData.Length); ServerStream.Flush(); IsSent = true; //Logger.Log(AppID + " : " + dpDataToSend.Message); } else { Logger.Log("Server NA / Non-Writable on SendDataToServer "); Logger.Log("Server " + Client.Connected + " / " + ServerStream.CanWrite + " on SendDataToServer"); } } catch (Exception sendEx) { Logger.Log("SendDataToServer Err" + sendEx.Message.ToString()); } return(IsSent); }
internal void StopAll() { try { if (Client != null) { Client.Close(); Client = null; } } catch (Exception ex1) { Logger.Log("TCP StopAll." + ex1.Message); } try { if (ServerStream != null) { ServerStream.Close(); ServerStream.Dispose(); ServerStream = null; } } catch (Exception ex1) { Logger.Log("TCP StopAll." + ex1.Message); } }
private void DisconnectFromServer() { ServerStream.Dispose(); ServerStream = null; serverSocket.Dispose(); serverSocket = null; }
//Bo add static public void UpdateNewScore(int score, ServerStream stream, Actions.DelegateBegin onBegin, Actions.DelegateDone onDone, Actions.DelegateCancel onCancel) { Actions.ActionFacebookPostHighScoreArg arg_post_score = new Actions.ActionFacebookPostHighScoreArg(); Actions.ActionFacebookPostHighScore action_post_score = new Actions.ActionFacebookPostHighScore(); _stream_wating.Add(action_post_score, stream); try { if (onBegin != null) { action_post_score.OnBegin += onBegin; } action_post_score.OnDone += action_post_score_OnDone; if (onCancel != null) { action_post_score.OnCancel += onCancel; } arg_post_score.Score = score; stream.Status = (int)ServerStreamStatus.STREAM_STATUS_REQUESTING; action_post_score.Do(arg_post_score); } catch (Exception ex) { stream.ErrorCode = 1; stream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; } }
void support_act_OnDone(object sender, ActionArgs e) { ActionSupportAccountAskArgs arg = e as ActionSupportAccountAskArgs; ServerStream sv = _stream_wating[(Actions.Action)sender]; _stream_wating.Remove((Actions.Action)sender); try { UInt64 acc_id = UInt64.Parse(arg.AccountId); if (acc_id > 3000000000000000000) { PlayerInfomation.FacebookId = (acc_id - 3000000000000000000).ToString(); } else if (acc_id > 300000000000000000) { PlayerInfomation.FacebookId = (acc_id - 300000000000000000).ToString(); } else { PlayerInfomation.FacebookId = arg.AccountId.ToString(); } PlayerInfomation.UserId = arg.AccountId.ToString(); PlayerInfomation.FacebookUserName = "******"; sv.ErrorCode = 0; sv.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; } catch (Exception ex) { sv.ErrorCode = 1; sv.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; } }
public async Task Connect() { await ServerStream.WaitForConnectionAsync(); ServerStreamReader = new StreamReader(ServerStream); ServerStreamWriter = new StreamWriter(ServerStream); }
public static void login(ServerStream stream, Actions.DelegateBegin onBegin, Actions.DelegateDone onDone, Actions.DelegateCancel onCancel) { Actions.ActionTwitterLoginArgs arg = new Actions.ActionTwitterLoginArgs(AppObject.RootLayout); Actions.ActionTwitterLogin action_twlogin = new Actions.ActionTwitterLogin(); _stream_wating.Add(action_twlogin, stream); try { if (onBegin != null) { action_twlogin.OnBegin += onBegin; } action_twlogin.OnDone += action_twitter_login_OnDone; if (onDone != null) { action_twlogin.OnDone += onDone; } action_twlogin.OnCancel += action_twitter_login_OnCancel; if (onCancel != null) { action_twlogin.OnCancel += onCancel; } stream.Status = (int)ServerStreamStatus.STREAM_STATUS_REQUESTING; ////ApplicationInfomation.onPopupOn(); AppObject.PopupOfAction.Add(new Actions.ActionArgLinker(action_twlogin, arg)); action_twlogin.Do(arg); } catch (Exception ex) { stream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; } }
public async Task Main() { await Connect(); while (ServerStream.IsConnected) { try { var json = await ServerStreamReader.ReadLineAsync(); IPCMessage data = JsonConvert.DeserializeObject <IPCMessage>(json); var method = data.TargetMethod.GetMethod(); if (method.DeclaringType == typeof(MainClass)) { method.Invoke(MainClass, data.Arguments); } } catch (ArgumentNullException) { ServerStream.Disconnect(); await Connect(); } catch (Exception ex) { Console.WriteLine(ex); } } }
private static void twfeed_login_cancel(object sender, Actions.ActionArgs e) { if (TWFeedStreamWaiting != null) { TWFeedStreamWaiting.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; TWFeedStreamWaiting = null; } }
public void CheckUI() { if (ServerStream.dataList != null) { if (ServerStream.dataList.Count == 0) { displayParallelCoord(); //displayLayers(); //scatterplot.read(); //ENVmds.read(); FilteringTypes.filter_nodes(filterPrefab, textPrefab); //datasetB(); //stopTracking(); //LayeredMapping.furtherData(); //LayeredMapping.hideLayers(); //initHistory(false); //initHistory(true); //initHistory(false); //initHistory(false); //makeENVlayer(1); //makeENVlayer(2); //makeENVlayer(3); //SetEdgeBundle(); //print("MI count" + instantiators.Count); //smokey_minimize(); // initialize the view } if (ServerStream.dataList.Count != 0) { specie_name = ServerStream.dataList[0]; ServerStream.LoadNext(); BuildInstantiator(); if (ServerStream.dataList.Count == 0 && ONCEE == false) { ServerStream.newData(); displayLayers(); instantiators.Clear(); if (ONCEE == false) { //print("sdsad"+instantiators.Count); //instantiators.Clear(); } batchCount = allHabitats.Count; AllbatchCounts.Add(eachSpecieCount); print(eachSpecieCount); //eachSpecieCount = 0; ModelInstantiator.hideAll(); } if (ServerStream.dataList.Count == 0 && ONCEE == true) { displayLayers(); AllbatchCounts.Add(eachSpecieCount); print(eachSpecieCount); //instantiators.Clear(); } CheckUI(); } } }
void support_act_OnCancel(object sender, ActionArgs e) { ActionSupportAccountAskArgs arg = e as ActionSupportAccountAskArgs; ServerStream sv = _stream_wating[(Actions.Action)sender]; _stream_wating.Remove((Actions.Action)sender); sv.ErrorCode = 1; sv.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; }
private static void twfeed_login_done(object sender, Actions.ActionArgs e) { ServerStream stream = TWFeedStreamWaiting; TWFeedStreamWaiting = null; if (stream != null) { twfeed(stream); } }
static private void action_fbinfo_stream_onCancel(object sender, Actions.ActionArgs e) { try { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; _stream_wating.Remove((Actions.Action)sender); ////ApplicationInfomation.onPopupOff(); } catch (Exception ex) { } }
private ServerStream SetupServer(out Executor executor, out Task session) { var server = new ServerStream(); var client = new Client((_, __) => Task.FromResult <Stream>(server)); executor = new Executor(); session = executor.Start(client); return(server); }
public override void process(ClientServiceRequest request, ServerStream svstream) { if (request == CurrentGameServiceRequest.Exsample) { } else { svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; } }
static private void action_fbinfo_stream_do(ServerStream onstream) { Actions.ActionFacebookGetInfomationArgs arg = new Actions.ActionFacebookGetInfomationArgs(); Actions.ActionFacebookGetInfomation action_fbinfo = new Actions.ActionFacebookGetInfomation(); _stream_wating.Add(action_fbinfo, onstream); try { action_fbinfo.OnDone += action_fbinfo_stream_onDone; action_fbinfo.OnCancel += action_fbinfo_stream_onCancel; onstream.Status = (int)ServerStreamStatus.STREAM_STATUS_REQUESTING; action_fbinfo.Do(arg); } catch (Exception ex) { onstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; ////ApplicationInfomation.onPopupOff(); } }
public static void twfeed(ServerStream svstream) { if (TwitterClient.isAlreadyLoggedIn() && TWFeedStreamWaiting == null) { StreamBuffer buf = new StreamBuffer(svstream.PostData); string message = buf.readSStr(); TWFeedStreamWaiting = svstream; ////TwitterClient.PostTweet(message + " " + AppObject.AppLink, action_twitter_feed_callback); } else { TWFeedStreamWaiting = svstream; login(TWFeedStreamWaiting, null, twfeed_login_done, twfeed_login_cancel); } }
private void UpdateClient() { while (Client.DataAvailable) { var packet = ClientStream.ReadPacket(Craft.Net.PacketDirection.Serverbound); Log.LogPacket(packet, true); if (packet is EncryptionKeyResponsePacket) { if (!FinializeClientEncryption((EncryptionKeyResponsePacket)packet)) { if (ConnectionClosed != null) { ConnectionClosed(this, null); } Worker.Abort(); return; } } else { var eventArgs = new IncomingPacketEventArgs(packet, true); if (IncomingPacket != null) { IncomingPacket(this, eventArgs); } lock (Server) { if (!eventArgs.Handled) { ServerStream.WritePacket(packet, Craft.Net.PacketDirection.Serverbound); } // We use a BufferedStream to make sure packets get sent in one piece, rather than // a field at a time. Flushing it here sends the assembled packet. Server.Flush(); } if (packet is DisconnectPacket) { Console.WriteLine("Client disconnected: " + ((DisconnectPacket)packet).Reason); if (ConnectionClosed != null) { ConnectionClosed(this, null); } Worker.Abort(); } } } }
void support_global_act_OnDone(object sender, ActionArgs e) { ActionSupportAccountAskArgs arg = e as ActionSupportAccountAskArgs; ServerStream sv = _stream_wating[(Actions.Action)sender]; _stream_wating.Remove((Actions.Action)sender); int buff_len = 2 + 2 * arg.AccountId.Length; StreamBuffer buff = new StreamBuffer(buff_len); buff.writeSStr(arg.AccountId); sv.ResponseData = buff.Data; sv.ErrorCode = 0; sv.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; }
public List <DataPacket> ReceiveMessage() { List <DataPacket> dpsReceived = new List <DataPacket>(); try { if (Client.Connected && ServerStream.DataAvailable) { byte[] inStream = new byte[Client.ReceiveBufferSize]; int recb = ServerStream.Read(inStream, 0, Client.ReceiveBufferSize); if (recb == 0) { return(null); } string returndata = System.Text.Encoding.ASCII.GetString(inStream); returndata = returndata.Replace("\0", ""); if (returndata.Length > 0) { Logger.Log("ReceivedData*** " + returndata); foreach (string oneData in returndata.Split("|".ToCharArray())) { try { if (!string.IsNullOrEmpty(oneData) && oneData != "|" && oneData != " ") { dpsReceived.Add(JsonConvert.DeserializeObject <DataPacket>(oneData)); } } catch (Exception OneParseEx) { Logger.Log("OneParseEx ", OneParseEx); } } } } } catch (Exception ex) { Logger.Log("Error while receiving. " + ex.Message); } return(dpsReceived); }
static public void action_post_score_OnDone(object sender, Actions.ActionArgs e) { Actions.ActionFacebookPostHighScoreArg arg = e as Actions.ActionFacebookPostHighScoreArg; if (arg != null) { if (arg.score_inform.success) { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; _stream_wating.Remove((Actions.Action)sender); } else { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; _stream_wating.Remove((Actions.Action)sender); } } }
static private void action_fblogin_stream_OnCancel(object sender, Actions.ActionArgs e) { try { ServerStream svstream = _stream_wating[(Actions.Action)sender]; ActionFacebookLoginArgs arg = e as ActionFacebookLoginArgs; foreach (ActionArgLinker link in AppObject.PopupOfAction) { if (link.AttachArg == arg) { AppObject.PopupOfAction.Remove(link); break; } } svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; _stream_wating.Remove((Actions.Action)sender); ////ApplicationInfomation.onPopupOff(); } catch (Exception ex) { } }
static private void action_fbinfo_stream_onDone(object sender, Actions.ActionArgs e) { try { Actions.ActionFacebookGetInfomationArgs fbarg = e as Actions.ActionFacebookGetInfomationArgs; if (fbarg != null) { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; _stream_wating.Remove((Actions.Action)sender); } else { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; _stream_wating.Remove((Actions.Action)sender); } ////ApplicationInfomation.onPopupOff(); } catch (Exception ex) { } }
static private void action_twitter_login_OnCancel(object sender, Actions.ActionArgs e) { try { Actions.ActionTwitterLoginArgs twarg = e as Actions.ActionTwitterLoginArgs; foreach (ActionArgLinker link in AppObject.PopupOfAction) { if (link.AttachArg == twarg) { AppObject.PopupOfAction.Remove(link); break; } } ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; _stream_wating.Remove((Actions.Action)sender); ApplicationInfomation.onPopupOff(); AppObject.PopupOfAction.Clear(); } catch (Exception ex) { } }
static private void action_fblogin_stream_OnDone(object sender, Actions.ActionArgs e) { try { Actions.ActionFacebookLoginArgs fbarg = e as Actions.ActionFacebookLoginArgs; ActionFacebookLoginArgs arg = e as ActionFacebookLoginArgs; foreach (ActionArgLinker link in AppObject.PopupOfAction) { if (link.AttachArg == arg) { AppObject.PopupOfAction.Remove(link); break; } } if (fbarg != null && (fbarg.IsCancelByLogedin || (fbarg.Result != null && fbarg.Result.IsSuccess))) { if (fbarg.IsCancelByLogedin) { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; ApplicationInfomation.onPopupOff(); } else { FacebookClient.Instance.Token = fbarg.Result.Token; FacebookClient.Instance.TokenExprite = fbarg.Result.TokenExprite; FacebookClient.Instance.SaveUserData(); ServerStream svstream = _stream_wating[(Actions.Action)sender]; action_fbinfo_stream_do(svstream); } } else { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; _stream_wating.Remove((Actions.Action)sender); ApplicationInfomation.onPopupOff(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
private void RejectConnect(ServerStream stream) { long sid = stream.SessionId; bool removed = false; lock (this.strmap) { if (this.strmap.TryGetValue(sid, out ServerStream ss)) { if (ss == stream) { this.strmap.Remove(sid); removed = true; } } } if (removed) { this.udp.SendToClient(stream.RemoteEndPoint, UDPMeta.CreateSessionError(sid, "Reject")); } }
static private void action_twitter_login_OnDone(object sender, Actions.ActionArgs e) { try { Actions.ActionTwitterLoginArgs twarg = e as Actions.ActionTwitterLoginArgs; foreach (ActionArgLinker link in AppObject.PopupOfAction) { if (link.AttachArg == twarg) { AppObject.PopupOfAction.Remove(link); break; } } if (twarg != null && (twarg.IsCancelByLogedin || (twarg.Result != null && twarg.Result.IsSuccess))) { if (twarg.IsCancelByLogedin) { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; _stream_wating.Remove((Actions.Action)sender); ////ApplicationInfomation.onPopupOff(); } else { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_RESPONSED; _stream_wating.Remove((Actions.Action)sender); ////ApplicationInfomation.onPopupOff(); } } else { ServerStream svstream = _stream_wating[(Actions.Action)sender]; svstream.Status = (int)ServerStreamStatus.STREAM_STATUS_ERROR; _stream_wating.Remove((Actions.Action)sender); //// ApplicationInfomation.onPopupOff(); } AppObject.PopupOfAction.Clear(); } catch (Exception ex) { } }
void ProcessSender() { try { do { mreSender.WaitOne(); if (!abort) { if (ServerStream != null) { while (SendQueue.Count > 0) { if (_log.IsInfoEnabled) { _log.Info("Sending data to server"); } byte[] buff = SendQueue.Dequeue(); ServerStream.Write(buff, 0, buff.Length); } } } if (!abort) { mreSender.Reset(); } } while (!abort); } catch (ThreadAbortException) { } catch (SocketException e) { if (_log.IsWarnEnabled) { _log.Warn("Exception from socket", e); } } }